package com.project.medical.sys.service.impl;

import com.project.medical.sys.common.utils.PageUtils;
import com.project.medical.sys.common.utils.Query;
import com.project.medical.sys.entity.TokenEntity;
import com.project.medical.sys.util.Encoder;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.project.medical.sys.dao.ManagerDao;
import com.project.medical.sys.entity.ManagerEntity;
import com.project.medical.sys.service.ManagerService;


@Service("managerService")
public class ManagerServiceImpl extends ServiceImpl<ManagerDao, ManagerEntity> implements ManagerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ManagerEntity> page = this.page(
                new Query<ManagerEntity>().getPage(params),
                new QueryWrapper<ManagerEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public TokenEntity login(Map<String, Object> params) {

        String password =(String) params.get("pass");
        String user =params.get("user").toString();
        System.out.println(user+password);
        if (StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(password)){
            ManagerEntity res = this.getOne(
                    new QueryWrapper<ManagerEntity>().eq("phone", user)
                            .eq("password", password)
            );
            if (res!=null){
                TokenEntity tokenEntity = new TokenEntity();
                tokenEntity.setName(res.getName());
                String encoder = Encoder.encoder(res.getPassword());
                tokenEntity.setPassword(encoder);
                tokenEntity.setPhone(res.getPhone());
                tokenEntity.setType("admin");
                tokenEntity.setId(res.getId());
                return tokenEntity;
            }else {
                return null;
            }
        }
        return null;

    }

    @Override
    public boolean checkToken(Map<String, Object> params) {
        String password = (String) params.get("password");
        String phone = (String) params.get("phone");
        if (StringUtils.isNotEmpty(password) && StringUtils.isNotEmpty(phone)){
            ManagerEntity res = this.getOne(new QueryWrapper<ManagerEntity>().eq("password", password).eq("phone",phone));
            if (res != null) return true;
        }
        return false;
    }

    @Override
    public ManagerEntity getByPhone(String phone) {

        return baseMapper.selectOne(new QueryWrapper<ManagerEntity>().eq("phone",phone));
    }

}